﻿Public Class frmFundMap

#Region "Properties"

    Private _CurrentFund As Fund
    Public Property CurrentFund() As Fund
        Get
            Return _CurrentFund
        End Get
        Set(ByVal value As Fund)
            _CurrentFund = value
        End Set
    End Property

    Private _CurrentMap As dsFund.FundBrokerMapRow
    Public Property CurrentMap() As dsFund.FundBrokerMapRow
        Get
            Return _CurrentMap
        End Get
        Set(ByVal value As dsFund.FundBrokerMapRow)
            _CurrentMap = value
        End Set
    End Property

    Private _FundError As ErrorHandler
    Public Property FundError() As ErrorHandler
        Get
            Return _FundError
        End Get
        Set(ByVal value As ErrorHandler)
            _FundError = value
        End Set
    End Property

    Private _IsNew As Boolean
    Public Property IsNew() As Boolean
        Get
            Return _IsNew
        End Get
        Set(ByVal value As Boolean)
            _IsNew = value
        End Set
    End Property

    Private _IDToFind As Integer
    Public Property IDToFind() As Integer
        Get
            Return _IDToFind
        End Get
        Set(ByVal value As Integer)
            _IDToFind = value
        End Set
    End Property

#End Region

#Region "Button Clicks"

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        If Me.chkGlobalRename.Checked Then Me.CurrentFund.MappedToName = Me.txtGlobalRename.Text

        If Me.IsNew Then
            Me.CurrentFund.ID = My.Settings.AllFunds.Count
            My.Settings.AllFunds.Add(Me.CurrentFund)
        Else
            Me.IDToFind = Me.CurrentFund.ID
            Dim iID As Integer = My.Settings.AllFunds.FindIndex(AddressOf FindID)
            My.Settings.AllFunds(iID) = Me.CurrentFund
        End If

        Me.tsslFundMap.Text = "Fund changes saved."
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub

    Private Sub chkNoUpload_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkNoUpload.CheckedChanged
        If Me.chkNoUpload.Checked = True Then
            Me.gbFundRouting.Enabled = False
            Me.gbGlobalRename.Enabled = False
        Else
            Me.gbFundRouting.Enabled = True
            Me.gbGlobalRename.Enabled = True
        End If
        Me.CurrentFund.IsNoneMapped = Me.chkNoUpload.Checked
    End Sub
    Private Sub chkGlobalRename_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGlobalRename.CheckedChanged
        If Me.chkGlobalRename.Checked Then
            Me.txtGlobalRename.Enabled = True
            Me.rbFundRename.Enabled = False
        Else
            Me.txtGlobalRename.Enabled = False
            Me.rbFundRename.Enabled = True
        End If
        Me.CurrentFund.IsGlobalNameMap = Me.chkGlobalRename.Checked

    End Sub
    
    Private Sub cboBrokerMap_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBrokerMap.SelectedValueChanged
        Try
            If Me.rbFundRout.Checked Then
                Dim b As Broker = Me.cboBrokerMap.SelectedItem
                Me.cboBrokerMapTo.Items.Clear()
                For Each brk As Broker In My.Settings.AllBrokers
                    If brk IsNot b Then Me.cboBrokerMapTo.Items.Add(brk)
                Next

                Dim brNoUpload As New Broker
                brNoUpload.BrokerName = "no upload"
                Me.cboBrokerMapTo.Items.Insert(0, brNoUpload)

                If b.BrokerName = "all" Then
                    Me.cboCCY.Enabled = False
                Else
                    Me.cboCCY.Enabled = True
                End If


            End If
        Catch ex As Exception
            FundError.ShowMsgbox(ex, "Broker Map Change")
        End Try

    End Sub
    Private Sub cmdAddRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRule.Click
        Try
            If Me.rbFundRout.Checked Then
                Dim drFundRout As dsFund.FundBrokerMapRow = Me.CurrentFund.MappingBroker.NewFundBrokerMapRow
                Dim brkFrom As Broker = Me.cboBrokerMap.SelectedItem
                Dim brkTo As Broker = Me.cboBrokerMapTo.SelectedItem
                With drFundRout
                    .FromBroker = brkFrom.BrokerName
                    .ToBroker = brkTo.BrokerName
                    If Not .CCY = String.Empty Then .CCY = Me.cboCCY.SelectedItem
                End With
                Me.CurrentFund.MappingBroker.AddFundBrokerMapRow(drFundRout)
            Else
                Dim drRename As dsFund.FundRenameRow = Me.CurrentFund.RenameCCYAndBroker.NewFundRenameRow
                Dim brkFrom As Broker = Me.cboBrokerMap.SelectedItem
                With drRename
                    .Broker = brkFrom.BrokerName
                    .NewName = Me.txtFundRename.Text
                    If Not .CCY = String.Empty Then .CCY = Me.cboCCY.SelectedItem
                End With
                Me.CurrentFund.RenameCCYAndBroker.AddFundRenameRow(drRename)
            End If
        Catch ex As Exception
            FundError.ShowMsgbox(ex, "Rule Add Error")
        End Try
    End Sub
    Private Sub rbFundRout_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbFundRout.CheckedChanged
        If Me.rbFundRout.Checked Then
            Me.lblFundRename.Visible = False
            Me.txtFundRename.Visible = False
            Me.lblBrokerMapFrom.Visible = True
            Me.lblBrokerMapFrom.Text = "Select broker to map from:"
            Me.lblBrokerMapTo.Visible = True
            Me.cboBrokerMapTo.Visible = True
            Me.cmdNDFDays.Visible = True
            Me.RefreshBrokerMap()
        End If
    End Sub

    Private Sub rbFundRename_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbFundRename.CheckedChanged
        If Me.rbFundRename.Checked Then
            Me.lblFundRename.Visible = True
            Me.txtFundRename.Visible = True
            Me.lblBrokerMapFrom.Text = "Select broker to rename from:"
            Me.lblBrokerMapTo.Visible = False
            Me.cboBrokerMapTo.Visible = False
            Me.cmdNDFDays.Visible = False
            Me.RefreshBrokerMap()
        End If
    End Sub
    Private Sub cmdNDFDays_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNDFDays.Click
        frmNDFDeets.Show()
    End Sub

#End Region

#Region "Public Methods"

    Public Overloads Sub Show(ByVal myFund As String)
        Me.FundError = New ErrorHandler()
        If Not myFund = String.Empty Then
            Me.lblFund.Text = Me.lblFund.Text & myFund
            Me.CurrentFund = My.Settings.AllFunds.GetFundByName(myFund)
            If Me.CurrentFund Is Nothing Then
                Me.IsNew = True
                Me.CurrentFund = New Fund(myFund) 'new fund to map
            Else
                Me.IsNew = False
                'editing existing fund
                PopulateFundDetails()
            End If
            Me.rbFundRout.Checked = True
        End If
        For Each b As Broker In My.Settings.AllBrokers
            Me.cboBrokerMap.Items.Add(b)
        Next

        Dim brkAll As New Broker
        brkAll.BrokerName = "all"
        Me.cboBrokerMap.Items.Insert(0, brkAll)

        If Me.chkGlobalRename.Checked = False Then Me.txtGlobalRename.Enabled = False

        MyBase.Show()
    End Sub
    Private Sub PopulateFundDetails()
        Me.chkGlobalRename.Checked = Me.CurrentFund.IsGlobalNameMap
        Me.chkNoUpload.Checked = Me.CurrentFund.IsNoneMapped
        Me.txtGlobalRename.Text = Me.CurrentFund.MappedToName
    End Sub

#End Region

#Region "Private Methods"
    'Predicate Delegate for Find Method
    Private Function FindID(ByVal fnd As Fund) As Boolean
        If fnd.ID = IDToFind Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub RefreshFunds()
        frmLoader.lbFundRouting.Items.Clear()
        For Each f As Fund In My.Settings.AllFunds
            frmLoader.lbFundRouting.Items.Add(f)
        Next
    End Sub
    Private Sub RefreshBrokerMap()
        Me.dgvBrokerMap.DataSource = Nothing
        If Me.rbFundRout.Checked Then
            Me.dgvBrokerMap.DataSource = Me.CurrentFund.MappingBroker
            Me.dgvBrokerMap.Columns(0).Visible = False
        Else
            Me.dgvBrokerMap.DataSource = Me.CurrentFund.RenameCCYAndBroker
            Me.dgvBrokerMap.Columns(0).Visible = False
        End If

    End Sub

    Private Sub frmFundMap_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        RefreshFunds()
    End Sub

#End Region


End Class
